package com.weixin.engine.interceptor;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 重写InitializingBean
 *
 * @author wangchunhui
 * @date 2023/6/5 17:56
 */
//@Configuration
public class HandlerMethodArgumentResolverAutoConfiguration implements InitializingBean {

    @Resource
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @Override
    public void afterPropertiesSet() throws Exception {
        List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
        if (CollUtil.isEmpty(argumentResolvers)) {
            return;
        }
        List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<>();
        for (HandlerMethodArgumentResolver argumentResolver : argumentResolvers) {
            if (argumentResolver instanceof RequestResponseBodyMethodProcessor) {
                RequestResponseBodyMethodProcessor processor = (RequestResponseBodyMethodProcessor) argumentResolver;
                customArgumentResolvers.add(new ArgumentResolver(processor));
            }
            customArgumentResolvers.add(argumentResolver);
        }
        requestMappingHandlerAdapter.setArgumentResolvers(customArgumentResolvers);

    }
}
